<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">





  <link rel="alternate" href="/atom.xml" title="renhao" type="application/atom+xml">






<meta name="description" content="如题描述，本文讨论闭包以及js中的变量声明有函数声明、匿名函数等相关问题">
<meta property="og:type" content="article">
<meta property="og:title" content="js知识总结之预解析、匿名函数与闭包">
<meta property="og:url" content="https://huangrenhao.gitee.io/2018/11/10/js-yu-jie-xi-zuo-yong-yu/index.html">
<meta property="og:site_name" content="renhao">
<meta property="og:description" content="如题描述，本文讨论闭包以及js中的变量声明有函数声明、匿名函数等相关问题">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-05T08:15:59.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js知识总结之预解析、匿名函数与闭包">
<meta name="twitter:description" content="如题描述，本文讨论闭包以及js中的变量声明有函数声明、匿名函数等相关问题">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangrenhao.gitee.io/2018/11/10/js-yu-jie-xi-zuo-yong-yu/">





  <title>js知识总结之预解析、匿名函数与闭包 | renhao</title>
  








<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">renhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangrenhao.gitee.io/2018/11/10/js-yu-jie-xi-zuo-yong-yu/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="renhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="renhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">js知识总结之预解析、匿名函数与闭包</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-11-10T20:47:39+08:00">
                2018-11-10
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/js/" itemprop="url" rel="index">
                    <span itemprop="name">js</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/11/10/js-yu-jie-xi-zuo-yong-yu/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/11/10/js-yu-jie-xi-zuo-yong-yu/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.7k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  10
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>如题描述，本文讨论闭包以及js中的变量声明有函数声明、匿名函数等相关问题<a id="more"></a></p>
<h3 id="预解析"><a href="#预解析" class="headerlink" title="预解析"></a>预解析</h3><p>所谓预解析，就是在当前作用域之下，在js代码运行之前，会把带有var和function关键字的事先声明，但是不会<strong>赋值</strong>。</p>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">// 一道面试题</span>
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// Function a</span>
<span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// alert(10)</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>   
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 3</span>
a<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span>
<span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// TypeError: a is not a function</span>
</code></pre>
<p>所考到的知识点有两个：</p>
<ul>
<li>事先进行变量声明（即预解析，将变量提升到顶部）</li>
<li>函数声明优先于变量声明</li>
</ul>
<p>所以运行结果就是</p>
<ol>
<li>刚开始，a就是<code>function a(){alert(10)}</code>,就会看到这个函数</li>
<li><code>a()</code>，执行函数，<code>alert(10)</code></li>
<li>执行了<code>var a=3</code>,所以<code>alert(a)</code>显示3</li>
<li>然后<code>a</code>就不是一个函数，再往下执行<code>a()</code>就会报错</li>
</ol>
<pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">//类似相关</span>
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// undefined</span>
<span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// TypeError: a is not a function</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>   
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// Function a （需要注释掉第二行）</span>
a<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span>
<span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// TypeError: a is not a function</span>
</code></pre>
<p>这里有一个函数表达式，将一个匿名函数（没有名字的函数）赋值给一个变量，此时变量a的值就是指向函数对象的指针，从本质上来说和使用函数声明的方式定义一个有名字的函数是一样的，与第一个的区别就在这里，将函数的声明转化为了这种var型变量的声明，由于预解析只是进行事先声明，但并不进行赋值操作，所以一开始第一个<code>alert(a)</code>显示的是<code>underfind</code>，然后执行<code>a()</code>的时候会报错的原因和刚开始的一样，转化为var型之后就没有函数的声明优先于变量的声明，两个都是变量（即匿名函数声明等同于变量声明），这个时候<code>a</code>就是一个函数。</p>
<h4 id="暂时性死区"><a href="#暂时性死区" class="headerlink" title="暂时性死区"></a>暂时性死区</h4><p>什么是暂时性死区？只要区块（<strong>块级作用域</strong>）中存在 <strong>let、const</strong> 命令，这个区块对这些命令声明的变量，从一开始就形成封闭作用域，凡在<strong>声明之前使用</strong>这些变量就会报错 </p>
<p>为什么要有暂时性死区？暂时性死区和 let、const 语句不出现变量提升，主要是为了<strong>减少运行时错误</strong>，防止变量声明之前就使用这个变量</p>
<pre><code>var tmp = 123
if (true) {
    tmp = &#39;abc&#39; // referenceError
    let tmp
}

typeof x // referenceError
let x
typeof undeclared_variable // undefined
</code></pre><h3 id="预解析和作用域"><a href="#预解析和作用域" class="headerlink" title="预解析和作用域"></a>预解析和作用域</h3><pre class=" language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">//收录一些面试题</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    a<span class="token operator">=</span><span class="token number">3</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//结果是什么都没发生，因为要执行aa函数才会执行alert(0)</span>

<span class="token comment" spellcheck="true">//------------分割线1------------------</span>

<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    <span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">3</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//underfind  在aa函数里面，有var a=3，那么在aa作用域里面，就是把a这个变量声明提前，但是不会赋值，所以是underfind</span>

<span class="token comment" spellcheck="true">//------------分割线2------------------</span>

<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    <span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">3</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">//5,0   在函数体内，参数a的优先级高于变量a</span>

<span class="token comment" spellcheck="true">//------------分割线3------------------</span>

<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    a<span class="token operator">=</span><span class="token number">3</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">//5,0   在函数体内，执行alert(a)和a=3,修改的的并不是全局变量a，而是参数a</span>

<span class="token comment" spellcheck="true">//------------分割线4------------------</span>

<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    <span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">3</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">//5,3</span>
<span class="token comment" spellcheck="true">/*这个我也有点不理解，请教网上的说法，有两个答案(小伙伴如果知道怎么理解，欢迎在评论上指点)
1.参数优先级高于变量声明，所以 变量n的声明其实被忽略了，此时相当于*/</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>
   <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
   a<span class="token operator">=</span><span class="token number">3</span>
   <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">//2.形参和局部变量优先级一样，此时相当于</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token keyword">var</span> a<span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//先声明</span>
 a<span class="token operator">=</span><span class="token number">5</span>      <span class="token comment" spellcheck="true">//由于形参和变量名称一样，覆盖了！</span>
   <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
   a<span class="token operator">=</span><span class="token number">3</span>
   <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">//------------分割线5------------------</span>

<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">aa</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    a<span class="token operator">=</span><span class="token number">3</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token function">aa</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token function">alert</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">//underfind  3  0 </span>
<span class="token comment" spellcheck="true">/*首先，参数优先级高于全局变量，由于没传参数，所以是underfind
a=3，实际上修改的时形参a的值，并不是全局变量a，往下alert(a)也是形参a
最后的alert(a)，你懂的*/</span>
</code></pre>
<h3 id="匿名函数-闭包-JavaScript线程"><a href="#匿名函数-闭包-JavaScript线程" class="headerlink" title="匿名函数 闭包 JavaScript线程"></a>匿名函数 闭包 JavaScript线程</h3><pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//这个大家就要小心一点了，答案是5    55555</span>
<span class="token comment" spellcheck="true">//在setTimeout执行之前，for循环早就执行完了，i的值早已经是5了，所以一开始是执行，最后面的console.log(i);</span>
<span class="token comment" spellcheck="true">//在for循环的时候一下子自定义5个setTimeout，大概一秒后，就是输出55555</span>
</code></pre>
<p>首先要得到这个结果就必须对JavaScript线程有一定了解</p>
<p>这道题的考察点：JavaScript的单线程以及setTimeout的异步特性</p>
<blockquote>
<p>JavaScript引擎是单线程运行的，浏览器运行期间只有一个线程在运行js程序。浏览器的内核是多线程的，他们在内核控制下相互配合，以保持同步，一个浏览器至少实现三个常驻线程：JavaScript引擎线程，GUI渲染线程，浏览器事件触发线程。</p>
<ul>
<li><p>JavaScript引擎是基于事件驱动单线程执行的，js引擎一直等待着任务队列中任务的到来，然后加以处理，浏览器无论什么时候都只有一个js线程在运行js程序。</p>
</li>
<li><p>GUI渲染线程负责浏览器界面的渲染，当界面需要重绘的时候或者由于某种操作引发回流时，该线程就会执行，需要注意GUI渲染线程与js引擎是互斥的，当js引擎执行的时候GUI线程会被挂起，GUI更新会被保存在一个队列中，等到js引擎空闲时立即被执行</p>
</li>
<li><p>事件触发线程，当一个事件被触发时该线程会把事件添加到待处理的队列末尾，等待js引擎的处理。这些事件可来自js引擎当前执行代码块，如setTimeout，也可来自浏览器内核的其他线程如鼠标点击、ajax异步请求等，但由于js的单线程关系所有这些事件都得排队等待js引擎处理。</p>
</li>
<li><p>当线程中没有任何同步代码的前提下才会执行异步代码。</p>
</li>
</ul>
</blockquote>
<p>所以在上题之中setTimeout是异步的代码，即使是setTimeout中设置的等待时间为0也不会立即执行，而for循环代码是同步的，所以要等待for循环执行完以后才会执行setTimeout。</p>
<p>关于为什么都是5：因为setTimeout中的i是对外层i的引用，当setTimeout的代码被解释的时候，运行时只是记录了i的引用，而不是i的值。这样当setTimeout被触发的时候，setTimeout被同时取值，由于他们都是指向了外层的同一个i，而此时i的值是5，所以打印的都是5.</p>
<h3 id="这个问题更深层次的原因"><a href="#这个问题更深层次的原因" class="headerlink" title="这个问题更深层次的原因:"></a>这个问题更深层次的原因:</h3><pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">var</span> s <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">'input'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取整个网页的标签</span>
    window<span class="token punctuation">.</span>onload <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i <span class="token operator">&lt;</span> s<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>onclick <span class="token operator">=</span> <span class="token function">show</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">function</span> <span class="token function">show</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"你好,请"</span><span class="token operator">+</span>num<span class="token operator">+</span><span class="token string">"号嘉宾领奖"</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
</code></pre>
<p>这个例子实际运行点击无论哪个按钮，都会alert相同的值（s.length)</p>
<p>JavaScript没有块级作用域，用循环赋值给每一个 s[i] 的方法的参数都是一个i的引用，它们都指向同一个也是唯一一个i，所以最终无论点哪个按钮，alert 的都是同一个值，在这个循环中，i的值就等于 s.length。</p>
<p>解决方案：</p>
<pre class=" language-javascript"><code class="language-javascript">window<span class="token punctuation">.</span>onload<span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span> s<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>onclick<span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">show</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token comment" spellcheck="true">//采用的是直接调用函数表达式(免去了赋值再调用)</span>
 <span class="token punctuation">}</span>     <span class="token comment" spellcheck="true">//需要再函数体外围加上括号，然后再以括号调用</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">show</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"你好,请"</span><span class="token operator">+</span>num<span class="token operator">+</span><span class="token string">"号嘉宾领奖"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>采用的写法是利用IIFE（立即执行函数）模拟了一个块级作用域（函数有自己的作用域），实际上就是声明一个匿名函数，同时立即执行他自己。</p>
<p>匿名函数可以用来做闭包</p>
<pre class=" language-javascript"><code class="language-javascript">finciton <span class="token function">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">"a"</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> closure <span class="token operator">=</span> <span class="token function">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">closure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>F() 的返回值是一个内部函数，这个函数可以访问外部函数的作用域，也就是可以访问到外部函数的变量。 </p>
<p>注意：因为把返回的函数赋给了一个变量，虽然函数在执行完一瞬间会销毁其执行环境， 但是如果有闭包的话，闭包会保存外部函数的活动对象（变量），所以如果不 closure = null; 把对闭包的引用消除掉，闭包会一直存在内存中，垃圾收集器不会销毁闭包占用的内存。</p>
<p>这从某种程度上说也是<code>var</code>型变量的bug，<strong>即没有块级作用域和循环内变量过度共享</strong>，解决的方法在ES6中新提出的类型，即<code>let</code>和<code>const</code>，let声明的变量有块级作用域（所以循环内的let变量到循环外不能被使用）并且let声明的全局变量不是全局变量的属性（它们存在于一个不可见的块的作用域中，不能通过window.变量名的方式访问这些变量），let 没有变量提升。</p>
<h3 id="这个时候可以采用匿名函数-闭包的概念解决这个问题"><a href="#这个时候可以采用匿名函数-闭包的概念解决这个问题" class="headerlink" title="这个时候可以采用匿名函数 闭包的概念解决这个问题"></a>这个时候可以采用匿名函数 闭包的概念解决这个问题</h3><pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// j = i</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment" spellcheck="true">//这里的解析和上面基本一样，只是用闭包来记录每一次循环的i,</span>
<span class="token comment" spellcheck="true">//所以答案是5     01234</span>



<span class="token keyword">var</span> output <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token function">output</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 这里传过去的 i 值被复制了</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">//这里的解析和上面基本一样，把i当参数传进output，记录每一次循环的i,</span>
<span class="token comment" spellcheck="true">//所以答案是5     01234</span>
</code></pre>
<p>闭包、this常见面试题：</p>
<blockquote>
<p><a href="https://www.cnblogs.com/xxcanghai/p/4991870.html" target="_blank" rel="noopener">参考</a></p>
</blockquote>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span>o<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    fun<span class="token punctuation">:</span><span class="token keyword">function</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">fun</span><span class="token punctuation">(</span>m<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  a<span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  a<span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  a<span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  c<span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  c<span class="token punctuation">.</span><span class="token function">fun</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//问:三行a,b,c的输出分别是什么？</span>
</code></pre>
<pre class=" language-javascript"><code class="language-javascript"><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    say<span class="token punctuation">:</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> objTemp <span class="token operator">=</span> <span class="token punctuation">{</span>
      a<span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    say<span class="token punctuation">:</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> obj2 <span class="token operator">=</span> <span class="token punctuation">{</span>a<span class="token punctuation">:</span><span class="token number">30</span><span class="token punctuation">}</span>
obj<span class="token punctuation">.</span>say<span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// obj 对象不能产生作用域，箭头函数中的 this 始终指向其父作用域中的 this</span>
objTemp<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
objTemp<span class="token punctuation">.</span>say<span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>obj2<span class="token punctuation">)</span>
</code></pre>
<pre class=" language-js"><code class="language-js"><span class="token keyword">function</span> fun <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span>
                                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> f <span class="token operator">=</span> fun<span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span>name<span class="token punctuation">:</span> <span class="token string">'foo'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> t1 <span class="token operator">=</span> f<span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span>name<span class="token punctuation">:</span> <span class="token string">'bar'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> t2 <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span>name<span class="token punctuation">:</span> <span class="token string">'baz'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> t3 <span class="token operator">=</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span>name<span class="token punctuation">:</span> <span class="token string">'qux'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<pre class=" language-js"><code class="language-js"><span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">'global'</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>name  <span class="token operator">=</span> name<span class="token punctuation">;</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>sayName <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> personA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">'aaa'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> personB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">'bbb'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
personA<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
personB<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/11/25/d3-xue-xi-ji-chu-bu-fen/" rel="prev" title="d3学习">
                d3学习 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">renhao</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#预解析"><span class="nav-number">1.</span> <span class="nav-text">预解析</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#暂时性死区"><span class="nav-number">1.1.</span> <span class="nav-text">暂时性死区</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#预解析和作用域"><span class="nav-number">2.</span> <span class="nav-text">预解析和作用域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名函数-闭包-JavaScript线程"><span class="nav-number">3.</span> <span class="nav-text">匿名函数 闭包 JavaScript线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#这个问题更深层次的原因"><span class="nav-number">4.</span> <span class="nav-text">这个问题更深层次的原因:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#这个时候可以采用匿名函数-闭包的概念解决这个问题"><span class="nav-number">5.</span> <span class="nav-text">这个时候可以采用匿名函数 闭包的概念解决这个问题</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">renhao</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">265.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '2P1xqUWiCoNm14MH4yhURlJi-gzGzoHsz',
        appKey: 'uJN0uagHIVgXtBO1OuLV9Ban',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
